#include "../Hdr/Chunk.h"
#include "../Hdr/geomdef.h"
#include <SDL/SDL_opengl.h>
#include <iostream>

Eternal::Scene::Chunk::Chunk()
{
	Verts = new float[16384];
	i_vCount = 0;
	gridLocX = gridLocZ = 0;
}

Eternal::Scene::Chunk::~Chunk()
{
	
}

/* Bottleneck */
void Eternal::Scene::Chunk::GenerateVertices()
{
	
	glDisableClientState(GL_VERTEX_ARRAY);
	i_vCount = 0;
	for ( int x = gridLocX;x < gridLocX + 16;x++ )
	{
		for ( int z = gridLocZ;z < gridLocZ + 16;z++ )
		{
			// Top face
	/*		Verts[i_vCount] = (-1)+x; Verts[i_vCount+1] = (-1); Verts[i_vCount+2] = (1)+z;
			i_vCount += 2;
			Verts[i_vCount] = (1)+x; Verts[i_vCount+1] = (-1); Verts[i_vCount+2] = (1)+z;
			i_vCount += 2;
			Verts[i_vCount] = (1)+x; Verts[i_vCount+1] = (-1); Verts[i_vCount+2] = (-1)+z;
			i_vCount += 2;
			Verts[i_vCount] = (-1)+x; Verts[i_vCount+1] = (-1); Verts[i_vCount+2] = (-1)+z;
			i_vCount += 2;*/
			
			for ( unsigned short i = 0;i < 12;i++ )	{
				Verts[i_vCount] = CubeFace_Top[i];
				if ( i % 3 == 0 )	{
					Verts[i_vCount] += x;
				}
				else if ( i % 3 == 2 )
				{
					Verts[i_vCount] += z;
				}
				i_vCount++;
			}
		}
	}
}

/* Also bottleneck */
void Eternal::Scene::Chunk::Render() const
{
//	printf("%i\n", i_vertCount);
	std::cout << i_vCount << std::endl;
/*	glPushMatrix();
	glTranslatef((float)gridLocX * 16, 2, (float)gridLocZ * 16);
	glVertexPointer(3, GL_FLOAT, 0, Verts);
	glDrawArrays(GL_QUADS, 0, i_vCount);
	glPopMatrix();*/
	glBegin(GL_QUADS);
	for ( unsigned short i = 0;i < i_vCount-1;i += 3 )
	{
		glVertex3f(Verts[i], Verts[i+2], Verts[i+1]);
	}
	glEnd();
}
